package de.tiw.pr.prototype.imp;

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.util.XMLProcessor;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.open.oasis.docs.wsbpel._2._0.process.abstract_.AbstractPackage;
import org.open.oasis.docs.wsbpel._2._0.process.executable.ExecutablePackage;
import org.open.oasis.docs.wsbpel._2._0.process.executable.TAssign;
import org.open.oasis.docs.wsbpel._2._0.process.executable.TDocumentation;
import org.open.oasis.docs.wsbpel._2._0.process.executable.TProcess;
import org.open.oasis.docs.wsbpel._2._0.process.executable.impl.DocumentRootImpl;
import org.open.oasis.docs.wsbpel._2._0.process.executable.impl.TProcessImpl;
import org.open.oasis.docs.wsbpel._2._0.process.executable.impl.TSequenceImpl;
import org.open.oasis.docs.wsbpel._2._0.process.executable.util.ExecutableXMLProcessor;



import de.tiw.pr.prototype.Log;
import de.tiw.pr.prototype.RepositoryEngine;
import de.tiw.pr.prototype.Status;
import de.tiw.pr.prototype.util.DataStore;

public class RepositoryEngineImp implements RepositoryEngine {

	private HbDataStore ds;
	private SessionFactory sf;
	public RepositoryEngineImp(){
		ds = DataStore.getInstance();
		this.ds.setEPackages(new EPackage[] { ExecutablePackage.eINSTANCE});
		this.ds.initialize();
		sf = ds.getSessionFactory();

	}
	public Status checkin(String processXML) {
		// TODO Auto-generated method stub
		return null;
	}

	public Status checkout(UUID processID) {
		Session s = this.sf.openSession();
		s.beginTransaction();
//		Process p = (Process)s.load(TDocumentation.class, new Long(1));
		Query query = s.createQuery("FROM TAssign_group");
		List l = query.list();
//		System.out.println(p.toString());
		return null;
	}
	//TODO: here is just for executable package, should also works with execute process
	//TODO: find the type of package according to the input xml file
	public Status create(String processXMLFileName) {
		String xmlFile = processXMLFileName;//"/Users/tingwang/Documents/workspace_jee/xsdtest/ship.xml";
		ExecutableXMLProcessor processor = null;
		Resource resource = null;
		
		//TODO: find out the type of process, abstract process or executable process
		try {
			processor =  new ExecutableXMLProcessor();
			resource = processor.load(new File(xmlFile).getAbsolutePath(), null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		EList<EObject> eos = resource.getContents();
		
		DocumentRootImpl dri = (DocumentRootImpl) eos.get(0);
		TProcess p = dri.getProcess();
//		System.out.println("the type is: " + eos.get(0).getClass().toString());

		//TODO: hard code
//		this.ds.setEPackages(new EPackage[] { ExecutablePackage.eINSTANCE});
//		this.ds.initialize();

		final SessionFactory sessionFactory = ds.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(p);
		session.getTransaction().commit();
		session.close();
		return null;
	}

	public Log getLog(UUID processID) {
		// TODO Auto-generated method stub
		return null;
	}

}
